package com.wanmait.recruit.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.mapper.PersonMapper;
import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.service.PersonService;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service("personService")
@Transactional(rollbackFor = Exception.class)
public class PersonServiceImpl implements PersonService {

    @Resource
    PersonMapper personMapper;
    @Override
    public Person checkEmailByCode(String code){

        Person person = personMapper.selectByCode(code);
        if(person!=null){

            person.setIsPass(true);
            personMapper.updateByPrimaryKeySelective(person);
            return person;
        }
        return null;
    }

    @Override
    public Person checkPersonName(String personName) {

        return personMapper.selectByPersonName(personName);
    }

    @Override
    public Person checkEmail(String email) {
        return personMapper.selectByEmail(email);
    }



    @Override
    public void insert(Person object) {
        personMapper.insertSelective(object);
    }

    @Override
    public void delete(Person object) {

    }

    @Override
    public void update(Person object) {

    }

    @Override
    public PageInfo<Person> search(int pageNum, int pageSize) {
        return null;
    }

    @Override
    public boolean isPersonRegister(String personName, String password) {
        //判断是邮箱的正则表达式
        String em = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";/*注：js和java用正则表达式不一样*/
        /*String em ="/^\\w+@\\w+\\.[A-Za-z]{2,3}(\\.[A-Za-z]{2,3})?$/";*/     /*js用正则表达式*/
        //编译正则表达式
        //Pattern pattern = Pattern.compile(em);
        //忽略大小写
        Pattern pattern = Pattern.compile(em,Pattern.CASE_INSENSITIVE);
        //判断字符串是否与邮箱匹配
        Matcher matcher = pattern.matcher(personName);
        boolean rs = matcher.matches();

        //判断是手机号的正则表达式
        String ph = "^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8}$";  /*java用验证手机号*/
        //String ph = "^[1][34578]\\d{9}$";
        //编译正则表达式
        //Pattern pattern1 = Pattern.compile(ph);
        //忽略大小写
        Pattern pattern1 = Pattern.compile(ph,Pattern.CASE_INSENSITIVE);
        //判断字符串是否与手机号匹配
        Matcher matcher1 = pattern1.matcher(personName);
        boolean rs1 = matcher1.matches();

        Person person = null;

        if (rs){//邮箱登录
            String email=personName;
            Person person1 = personMapper.selectByEmailAndPassword(email, password);
            if (person1!=null){return true;}
            else {return false;}
        }
        else if (rs1){//手机号登录
            String phone=personName;
            Person person1 = personMapper.selectByPhoneAndPassword(phone, password);
            if (person1!=null){return true;}
            else {return false;}
        }
        else {//用户名登录
            Person person1 = personMapper.selectByPersonNameAndPassword(personName, password);
            if (person1!=null){return true;}
            else {return false;}
        }
    }

    @Override
    public Person isPersonRegisterData(String personName, String password) {
        //判断是邮箱的正则表达式
        String em = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";/*注：js和java用正则表达式不一样*/
        /*String em ="/^\\w+@\\w+\\.[A-Za-z]{2,3}(\\.[A-Za-z]{2,3})?$/";*/     /*js用正则表达式*/
        //编译正则表达式
        //Pattern pattern = Pattern.compile(em);
        //忽略大小写
        Pattern pattern = Pattern.compile(em,Pattern.CASE_INSENSITIVE);
        //判断字符串是否与邮箱匹配
        Matcher matcher = pattern.matcher(personName);
        boolean rs = matcher.matches();

        //判断是手机号的正则表达式
        String ph = "^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8}$";  /*java用验证手机号*/
        //String ph = "^[1][34578]\\d{9}$";
        //编译正则表达式
        //Pattern pattern1 = Pattern.compile(ph);
        //忽略大小写
        Pattern pattern1 = Pattern.compile(ph,Pattern.CASE_INSENSITIVE);
        //判断字符串是否与手机号匹配
        Matcher matcher1 = pattern1.matcher(personName);
        boolean rs1 = matcher1.matches();

        Person person = null;

        if (rs){//邮箱登录
            String email=personName;
            return personMapper.selectByEmailAndPassword(email,password);

        }
        else if (rs1){//手机号登录
            String phone=personName;
            return personMapper.selectByPhoneAndPassword(phone,password);

        }
        else {//用户名登录
            return personMapper.selectByPersonNameAndPassword(personName,password);
        }
    }

    @Override
    public Person findByPersonName(String personName) {
        return personMapper.findByPersonName(personName);
    }

    @Override
    public void updatePersonInfo(Person person) {
        personMapper.updatePersonInfo(person);
    }
    @Override
    public void updatePersonPic(Person person)
    {
        Integer personId = person.getId();
        String pic = person.getPic();
        this.personMapper.updatePersonPic(personId, pic);
    }

    @Override
    public void updatePersonNameAndPassword(Person person) {
        Integer id=person.getId();
        String personName=person.getPersonName();
        String password=person.getPassword();
        personMapper.updatePersonNameAndPassword(id, personName, password);
    }

    @Override
    public boolean selectIsPersonExistence(String personName) {
        Person person = personMapper.selectByPersonName(personName);
        if (person!=null){
            return true;
        }else {
            return false;
        }
    }
    @Override
    public PageInfo<Person> findAll() {
        Integer pageNum= PageContext.getPageNum();
        int pageSize=2;
        PageHelper.startPage(pageNum,pageSize);
        ArrayList<Person> all = personMapper.findAll();
        PageInfo<Person> pageInfo=new PageInfo<>(all);
        return  pageInfo;
    }


}
